package com.yyjj.yyjj.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyjj.yyjj.acl.mapper.RoleMapper;
import com.yyjj.yyjj.acl.service.AdminRoleService;
import com.yyjj.yyjj.acl.service.RoleService;
import com.yyjj.yyjj.model.acl.AdminRole;
import com.yyjj.yyjj.model.acl.Role;
import com.yyjj.yyjj.vo.acl.RoleQueryVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService  {


    private final AdminRoleService adminRoleService;

    /**
     * 条件分页查询
     * @param pageParam
     * @param roleQueryVo
     * @return
     */
    @Override
    public IPage<Role> selectRolePage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {
        //获取条件值
        String roleName = roleQueryVo.getRoleName();
        LambdaQueryWrapper<Role> wrapper=new LambdaQueryWrapper<>();
        //判断条件值是否为空，不为空封装条件查询
        if (!StringUtils.isEmpty(roleName)){
            wrapper.like(Role::getRoleName,roleName);
        }
        //调用方法实现条件分页查询，返回分页对象
        return baseMapper.selectPage(pageParam, wrapper);
    }

    //获取所有对象，和根据用户id查询用户分配角色列表
    @Override
    public Map<String, Object> getRoleByAdminId(Long adminId) {
        //查寻所有的角色
        List<Role> allRolesList = baseMapper.selectList(null);
        //根据用户Id查询用户分配角色列表
        //根据用户id查询用户角色关系表
        LambdaQueryWrapper<AdminRole> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId,adminId);
        List<AdminRole> adminRoleList = adminRoleService.list(wrapper);
        //2.2通过第一步返回集合，获取所有角色id的列表List<Long>
        List<Long> roleIdsList = adminRoleList.stream().map(AdminRole::getRoleId).collect(Collectors.toList());
        //创建新的List集合，用于存储用户配置角色
        List<Role> assignRoleList=new ArrayList<>();
        //遍历所有角色列表allRolesList,得到每个角色
        for (Role role : allRolesList) {
            if (roleIdsList.contains(role.getId())){
                assignRoleList.add(role);
            }
        }
        Map<String,Object> result=new HashMap<>();
        result.put("allRolesList",allRolesList);
        result.put("assignRoles",assignRoleList);
        return result;
    }

    //为用户分配角色
    @Override
    public void saveAdminRole(Long adminId, Long[] roleIds) {
        //删除用户已经分配过的角色的数据
        LambdaQueryWrapper<AdminRole> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId,adminId);
        //根据用户id删除admin_role表里面对应的数据
        adminRoleService.remove(wrapper);
        //重新进行分配
        //遍历多个角色id，得到每个角色Id,拿着每个角色id加用户id进行添加
        List<AdminRole> adminRoleList=new ArrayList<>();
        for (Long roleId : roleIds) {
            AdminRole adminRole=new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roleId);
            adminRoleList.add(adminRole);
        }
        adminRoleService.saveBatch(adminRoleList);

    }
}
